package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.CD;
import com.jwinf.cdLibary.service.CDService;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;


import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 * @author jwinf
 * 操作光盘抽象类 抽象出了编辑光盘和添加光盘中共有的操作
 */
public abstract class OperateCdFrameController implements Initializable {
    @FXML
    protected Label codeIcon;
    @FXML
    protected Label nameIcon;
    @FXML
    protected Label sortIcon;
    @FXML
    protected Label numberIcon;
    @FXML
    protected TextField codetxt;
    @FXML
    protected TextField nametxt;
    @FXML
    protected TextField sorttxt;
    @FXML
    protected TextField numbertxt;
    @FXML
    protected Button submitbtn;
    @FXML
    protected Button cancelbtn;

    protected CDService cdService = new CDService();
    protected CD cd = new CD();

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();           // 组件初始化
        addListener();             // 添加监听器
    }

    // 组件初始化方法
    private void componentInit(){

        // 改变标签的icon图标样式
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/codeIcon.png"), 30, 30, false, false);
        codeIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/nameIcon.png"), 30, 30, false, false);
        nameIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/sortIcon.png"), 30, 30, false, false);
        sortIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/numberIcon.png"), 30, 30, false, false);
        numberIcon.setGraphic(new ImageView(image));
    }

    // 添加监听器
    private void addListener(){
        // 进入组件时改变图标样式
        WindowUtils.changeMouse(submitbtn);
        WindowUtils.changeMouse(cancelbtn);
    }

    // 返回光盘管理界面
    @FXML
    protected void cancel() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/LookCdFrame.fxml", "/icon/loginIcon.png", "光盘管理");
        WindowUtils.destroyStage(cancelbtn);
    }

    // 检查数据的合法性
    protected boolean check(String str, String message){
        if(str == null || str.isEmpty()){
            WindowUtils.showTip(message, "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return false;
        }
        return true;
    }

    // 交由子类实现具体操作
    @FXML
    protected abstract void submit() throws SQLException;


}
